package com.zgkxzx.数据结构.数组;

/**
 * 创建者   zgkxzx
 * 创建日期 2018/5/9.
 * 功能描述
 */
public class AutoIncreaseArray<T> {
    private final static int DEFAULT_SIZE = 10;
    private final static float DEFAULT_INCREATE_FATOR = 0.5f;
    private T[] ts;

    private int currentSize = DEFAULT_SIZE;

    private int index = 0;


    public AutoIncreaseArray() {
        this(DEFAULT_SIZE);
    }

    public AutoIncreaseArray(int size) {
        ts = (T[]) new Object[size];
        currentSize = size;
    }

    public void add(T t) {
        if (index > currentSize - 1) {
            dilatationCap();
        }

        ts[index++] = t;
    }

    //扩容
    private void dilatationCap() {
        //扩大的大小
        currentSize = (int) (currentSize * (1 + DEFAULT_INCREATE_FATOR));
        T[] tmp = (T[]) new Object[currentSize];

        //将原来的数组数据拷贝到新数组
        System.arraycopy(ts, 0, tmp, 0, ts.length);
        ts = tmp;
    }

    public void delete(T t){
        //找到该数的索引，然后整体往左移动
    }

    public void print() {
        for (T t : ts)
            System.out.println(t);
    }

}
